package com.apimarket.service;

import com.apimarket.dao.PluginMapper;
import com.apimarket.entity.ApiInterfaceWithBLOBs;
import com.apimarket.entity.kong.Plugin;
import com.apimarket.enums.ExceptionEnum;
import com.apimarket.enums.InvalidTypeEnum;
import com.apimarket.enums.NetContextEnum;
import com.apimarket.exception.AMException;
import com.apimarket.exception.InvalidTypeException;
import com.apimarket.exception.NetContextException;
import com.apimarket.util.kong.KongOperationType;
import com.apimarket.util.kong.KongPlugin;
import com.apimarket.util.kong.KongServiceParameters;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;

/**
 * 具体处理Plugin相关操作服务层
 *
 * @author yyc
 */
@Service
public class PluginService {

    @Autowired
    private KongPlugin kongPlugin;
    @Autowired
    private PluginMapper pluginMapper;

    private static final Set<String> pluginTypeSet;

    static {
        //只允许添加这三种插件
        pluginTypeSet = new HashSet<>();
        pluginTypeSet.add("acl");
        pluginTypeSet.add("rate-limiting");
        pluginTypeSet.add("key-auth");
    }

    public Plugin addPlugin(Plugin plugin) throws IOException, URISyntaxException {
        // (pluginTypeSet.contains(plugin.getName())) return kongPlugin.addPlugin(plugin);
        if (pluginTypeSet.contains(plugin.getName())) return kongPlugin.addPluginAndJudge(plugin, KongOperationType.PLUGIN_ADD);
        else throw new InvalidTypeException(InvalidTypeEnum.PLUGIN_ERROR);
    }

    public Plugin updatePlugin(Plugin plugin) throws IOException, URISyntaxException {
        if(plugin.getProtocols()!=null && plugin.getProtocols().size()==0){
            //如果protocol传了一个空数组
            throw new AMException(ExceptionEnum.Protocols_IS_EMPTY_ARRY);
        }
        if (null != plugin.getId()) return kongPlugin.updatePlugin(plugin);
        else throw new NetContextException(NetContextEnum.ID_ERROR);
    }

    public void deletePlugin(String id) throws IOException, URISyntaxException {
        kongPlugin.deletePlugin(id);
    }

    public PageInfo<Map<String, Object>> findPageList(Integer pageSize, Integer pageCurrent, String name) {
        // 1.参数校验
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("当前页码有误");
        if(pageSize==null||pageSize<1)
            throw new IllegalArgumentException("当前页显示数目有误");
        PageHelper.startPage(pageCurrent,pageSize);
        List<Map<String, Object>> list=pluginMapper.findNameLike(name);
        if(list.size()==0)
            throw new AMException(ExceptionEnum.Plugin_NOT_FOUND);
        //丢给pageInfo处理，拿到总条数，页数，页码等等
        PageInfo<Map<String, Object>> pageInfo= new PageInfo<>(list);
        return pageInfo;
    }

    public List<Plugin> findServicePlugin(String id) {
        //List<Plugin> list=kongPlugin.findServicePlugin(id);
        //return list;
        return null;
    }

    public List<Plugin> findServicePlugins(String id) throws IOException, URISyntaxException {
        List<Plugin> result=kongPlugin.findServicePlugin(id);
        return result;
    }

    public List<Plugin> findRoutePlugins(String id) throws IOException, URISyntaxException {
        List<Plugin> list=kongPlugin.finRoutePlugins(id);
        return list;
    }
}
